package com.eamois.dao.manual.impl;

import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.eamois.dao.impl.BaseDaoImpl;
import com.eamois.dao.manual.ManualEngineDao;
import com.eamois.domain.manual.Engine;
import com.eamois.domain.manual.Manual;
import com.eamois.domain.manual.ManualEngine;

public class ManualEngineDaoImpl extends BaseDaoImpl<ManualEngine> implements ManualEngineDao {

	@SuppressWarnings("unchecked")
	public Collection<ManualEngine> findAllEngine() {
		// TODO Auto-generated method stub
		return (Collection<ManualEngine>) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session
								.createCriteria(ManualEngine.class);
						return criteria.list();
					}
				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<ManualEngine> findEngineByName(final String name,final String remark) {
		// TODO Auto-generated method stub
		return (Collection<ManualEngine>) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session
								.createCriteria(ManualEngine.class);
						if (name != null) {
							criteria.add(Restrictions.like("name", "%" + name+ "%"));
						}
						if (remark != null) {
							criteria.add(Restrictions.like("remark", "%" + remark+ "%"));
						}
						return criteria.list();
					}
				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public ManualEngine findEngine(final String name) {
		// TODO Auto-generated method stub
		return (ManualEngine) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session.createCriteria(ManualEngine.class);
						criteria.add(Restrictions.eq("name", name));
						if(criteria.list()!=null&&criteria.list().size()!=0){
							return criteria.list().get(0);
						}else{
							return null;
						}
					}
				});
	}

	@SuppressWarnings("unchecked")
	@Override
	public ManualEngine findEngineByManual(final Manual manual) {
		// TODO Auto-generated method stub
		return (ManualEngine) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						// TODO Auto-generated method stub
						Criteria criteria = session.createCriteria(ManualEngine.class);
						criteria.add(Restrictions.eq("manual", manual));
						if(criteria.list()!=null&&criteria.list().size()!=0){
							return criteria.list().get(0);
						}else{
							return null;
						}
					}
				});
	}

}
